/*
 * RecordDownloader.h
 *
 *  Created on: 2018年8月9日
 *      Author: zhengboyuan
 */

#ifndef RECORDDOWNLOADER_H_
#define RECORDDOWNLOADER_H_

#include "NetVideoSystem.h"
#include <string>
#include "LibMediaDecoder.h"


class RecordDownloaderSink
{
public:
	virtual ~RecordDownloaderSink() {}

	virtual void onDownloadEnd(void* obj, int reason) = 0;
};

class RecordDownloader : public nvs::StreamSink
{
public:
    RecordDownloader();
    virtual ~RecordDownloader();

	bool open(nvs::NetVideoSystem* system, const nvs::RecordItem& rec, const char* filepath);
	void close();
	bool isOpen();

	std::string getFilePath();

	void setSink(RecordDownloaderSink* sink);

	int64_t getBytes();

	int64_t getTime();

protected:
	///设置媒体格式
	virtual void setFormat(const nvs::StreamFormat& fmt);

	///发送媒体数据
	virtual int writePacket(const nvs::StreamPacket& packet);

	///通知发送媒体数据结束
	virtual void endOfStream(int reason);

protected:
	bool isStreamOpen();
	void closeStream();

	void copyFormat(MDecode_Format& dest, nvs::StreamFormat& src);
	int convertCodec(int nvsCodec);

	bool openStream(nvs::StreamFormat& format);

	void onOpen();

protected:
	std::string	m_filepath;
	nvs::NetVideoSystem*	m_system;
	nvs::MediaStream*   m_stream;
	nvs::RecordItem		m_record;
	nvs::StreamFormat   m_format;
	bool	m_formatReady;
	HANDLE	m_handle;
	RecordDownloaderSink*	m_sink;
	int64_t	m_bytes;
	int64_t	m_time;

};

#endif /* RECORDDOWNLOADER_H_ */
